/**
 * 
 */
package cn.ad.server.web.servlet.v2.parser;

import cn.ad.server.common.utils.IPUtils;
import cn.ad.server.web.spring.model.IAdserverRequest;
import cn.ad.server.web.spring.model.IAdserverResponse;
import cn.ad.server.web.spring.model.TalkingDataRequest;
import cn.ad.server.web.utils.HttpUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * talkingData激活回调请求解析类
 * @author wangwei
 */

@Component("parser-talkingData")
public  class TalkingDataRequestParser extends HttpServletParserAdapter {

	/**
	 * 请求参数名--接口版本号
	 */
	public static final String PARAM_VERSION = "version";
	/**
	 * guid
	 */
	private static final String PARAM_GUID = "guid";

	/**
	 * 素材id
	 */
	private static final String PARAM_MID = "mid";

	/**
	 * 创意id
	 */
	private static final String PARAM_CID = "cid";

	/**
	 * 策略id
	 */
	private static final String PARAM_SID = "sid";

	/**
	 * 广告位id
	 */
	private static final String PARAM_AID = "aid";

	/**
	 * 设备-androidid_md5
	 */
	private static final String PARAM_ANDROIDID_MD5 = "androidid_md5";

	/**
	 * 设备-androidid
	 */
	private static final String PARAM_ANDROIDID = "androidid";

	/**
	 * 设备-imei_md5
	 */
	private static final String PARAM_IMEI_MD5 = "imei_md5";

	/**
	 * 设备-imei
	 */
	private static final String PARAM_IMEI = "imei";

	/**
	 * 设备-idfa_md5
	 */
	private static final String PARAM_IDFA_MD5 = "idfa_md5";

	/**
	 * 设备-idfa
	 */
	private static final String PARAM_IDFA = "idfa";

	/**
	 * 设备-mac_md5
	 */
	private static final String PARAM_MAC_MD5 = "mac_md5";

	/**
	 * 设备-mac
	 */
	private static final String PARAM_MAC = "mac";

	/**
	 * 设备-系统
	 */
	private static final String PARAM_OS = "os";

	
	/**
	 * 渠道参数名称，用于在路径获取请求字符串标识渠道的参数名称
	 */
	private static final String CHANNEL_PARAM_NAME = "ch";
	
	
	/* (non-Javadoc)
	 * @see cn.falconnect.adserver.adapter.HttpServletParser#parse(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public IAdserverRequest parse(HttpServletRequest request) throws AdRequestParseException {
		TalkingDataRequest adRquest = new TalkingDataRequest();
		adRquest.setIp(IPUtils.parseIp(request));
		adRquest.setUa(request.getHeader("user-agent"));
		readReqParam(request, adRquest);
		return adRquest;
	}
	
	/**
	 * 读取标准监测参数
	 * @param request
	 * @param trackingRequest
	 */
	public void readReqParam(HttpServletRequest request, TalkingDataRequest trackCommand)  throws AdRequestParseException{
		if(trackCommand == null){
			throw new NullPointerException("the argument AdCallBackRequest is null");
		}
		
		String os = request.getParameter(PARAM_OS);
		trackCommand.setOs(null2Empty(os));
		
		String mac = request.getParameter(PARAM_MAC);
		trackCommand.setMac(null2Empty(mac));
		
		String mac_md5 = request.getParameter(PARAM_MAC_MD5);
		trackCommand.setMac_md5(null2Empty(mac_md5));
		
		String idfa = request.getParameter(PARAM_IDFA);
		trackCommand.setIdfa(null2Empty(idfa));
		
		String idfa_md5 = request.getParameter(PARAM_IDFA_MD5);
		trackCommand.setIdfa_md5(null2Empty(idfa_md5));
		
		String imei = request.getParameter(PARAM_IMEI);
		trackCommand.setImei(null2Empty(imei));
		
		String imei_md5 = request.getParameter(PARAM_IMEI_MD5);
		trackCommand.setImei_md5(null2Empty(imei_md5));
		
		String androidid = request.getParameter(PARAM_ANDROIDID);
		trackCommand.setAndroidid(null2Empty(androidid));
		
		String androidid_md5 = request.getParameter(PARAM_ANDROIDID_MD5);
		trackCommand.setAndroidid_md5(null2Empty(androidid_md5));
		
		//以上必定一个
		
		String aid = request.getParameter(PARAM_AID);
		trackCommand.setAid(null2Empty(aid));
		
		String sid = request.getParameter(PARAM_SID);
		trackCommand.setSid(null2Empty(sid));
		
		String cid = request.getParameter(PARAM_CID);
		trackCommand.setCid(null2Empty(cid));
		
		String mid = request.getParameter(PARAM_MID);
		trackCommand.setMid(null2Empty(mid));
		
		String guid = request.getParameter(PARAM_GUID);
		trackCommand.setGuid(null2Empty(guid));
		
		String ch = getChannelFromUriPath(request);
		
		trackCommand.setCh(null2Empty(ch));
		
		trackCommand.setChannelID(getChannelFromUriPath(request));
	}


	/**
	 * 如果null返回 空字符
	 * @param os
	 * @return
	 */
	private String null2Empty(String os){
		if(os==null){
			os="";
		}
		return os;
	}
	
	/**
	 * 从请求路径中获取渠道标识
	 * @param request
	 * @return
	 */
	private String getChannelFromUriPath(HttpServletRequest request){
		String ch = HttpUtils.getPathParam(request.getServletPath(), CHANNEL_PARAM_NAME);
		return ch;
	}
	
	protected final ObjectMapper objectMapper = new ObjectMapper();
	
	@Override
	public void writeResponse(IAdserverRequest adRequest, IAdserverResponse adResponse, HttpServletRequest request,
			HttpServletResponse response) throws AdRequestParseException {
//		try {
//			response.setContentType("application/json;charset=utf-8");
//			ServletOutputStream outputStream = response.getOutputStream();
//			objectMapper.writeValue(outputStream, adResponse);
//		} catch (JsonGenerationException e) {
//			throw new AdRequestParseException(e.getCause());
//		} catch (JsonMappingException e) {
//			throw new AdRequestParseException(e.getCause());
//		} catch (IOException e) {
//			throw new AdRequestParseException(e.getCause());
//		}
		
		response.setStatus(200);
		response.setHeader("Connection", "close");
		
	}
	
	
}
